package top.codedance.iotp.tools.terminal.ui;

import javax.swing.*;
import java.awt.*;
import java.net.URL;
import java.util.Enumeration;
import java.util.Formatter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author xuedianfu
 */
public class ResetNetwork extends javax.swing.JDialog {

    private ShellFrame sf;

    /**
     * Creates new form ResetNetwork
     */
    public ResetNetwork(ShellFrame sf) {
        super(sf, true);
        this.sf = sf;
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(this.sf);
        setMinimumSize(new Dimension(421, 286));
        setTitle("重置网络命令生成器");
    }

    public void initData(String cmd){
        if(cmd == null || cmd.isEmpty()) {
            this.jrb_enable.setSelected(true);
            this.jtf_networkName.setText("");
        }else{
            Pattern p1 = Pattern.compile("netsh\\s+interface\\s+set\\s+interface\\s+\"(\\S+)\"\\s+(.+)");
            Matcher matcher1 = p1.matcher(cmd);
            if(matcher1.find()) {
                Enumeration<AbstractButton> elements = this.bg_type.getElements();
                while (elements.hasMoreElements()){
                    AbstractButton abstractButton = elements.nextElement();
                    if(abstractButton.getActionCommand().equals(matcher1.group(2))){
                        abstractButton.setSelected(true);
                        break;
                    }
                }
                this.jtf_networkName.setText(matcher1.group(1));
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        bg_type = new javax.swing.ButtonGroup();
        jrb_enable = new javax.swing.JRadioButton();
        jrb_disable = new javax.swing.JRadioButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jtf_networkName = new javax.swing.JTextField();
        jb_save = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(180, 130));
        setMinimumSize(new java.awt.Dimension(180, 130));
        setPreferredSize(new java.awt.Dimension(180, 130));
        setResizable(false);

        bg_type.add(jrb_enable);
        jrb_enable.setSelected(true);
        jrb_enable.setText("开启");
        jrb_enable.setActionCommand("enable");

        bg_type.add(jrb_disable);
        jrb_disable.setText("关闭");
        jrb_disable.setToolTipText("");
        jrb_disable.setActionCommand("disable");

        jLabel1.setText("操作类型：");

        jLabel2.setText("网卡名称：");

        jb_save.setText("保存");
        jb_save.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_saveActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(44, 44, 44)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jtf_networkName, javax.swing.GroupLayout.PREFERRED_SIZE, 213, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1)
                                .addGap(18, 18, 18)
                                .addComponent(jrb_enable)
                                .addGap(18, 18, 18)
                                .addComponent(jrb_disable))))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(139, 139, 139)
                        .addComponent(jb_save)))
                .addContainerGap(42, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jrb_enable)
                    .addComponent(jrb_disable)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtf_networkName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jb_save)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_saveActionPerformed
        if(this.jtf_networkName.getText().isEmpty()){
            JOptionPane.showMessageDialog(rootPane, "网卡名不能为空");
            return;
        }
        Formatter formater2 = new Formatter();
        formater2.format(
                "netsh interface set interface \"%s\" %s",
                this.jtf_networkName.getText(), this.bg_type.getSelection().getActionCommand()
        );
        sf.addCmd(formater2.toString());
        this.setVisible(false);
    }//GEN-LAST:event_jb_saveActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup bg_type;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JButton jb_save;
    private javax.swing.JRadioButton jrb_disable;
    private javax.swing.JRadioButton jrb_enable;
    private javax.swing.JTextField jtf_networkName;
    // End of variables declaration//GEN-END:variables
}
